package com.ysg.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysg.R;
import com.ysg.utils.YDisplayUtil;
import com.ysg.utils.YStringUtil;

/**
 * 标题栏
 */
public class YTopBottomText extends RelativeLayout {

    private TextView tvTop, tvBottom;
    private ImageView ivTop, ivBottom;
    private View parent;
    private int defColor = 0xFFFFFFFF;// 默认颜色

    public YTopBottomText(Context context) {
        super(context);
    }

    public YTopBottomText(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.widget_top_bottom_text, this);
        parent = view.findViewById(R.id.parent);
        tvTop = view.findViewById(R.id.tvTop);
        tvBottom = view.findViewById(R.id.tvBottom);
        ivTop = view.findViewById(R.id.ivTop);
        ivBottom = view.findViewById(R.id.ivBottom);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YTopBottom);
        int topIcon = array.getResourceId(R.styleable.YTopBottom_topIcon, 0);
        String topText = array.getString(R.styleable.YTopBottom_topText);
        int bottomIcon = array.getResourceId(R.styleable.YTopBottom_bottomIcon, 0);
        String bottomText = array.getString(R.styleable.YTopBottom_bottomText);

        float topTextSize = array.getDimensionPixelSize(R.styleable.YTopBottom_topTextSize, dip2px(context, 0));
        float bottomTextSize = array.getDimensionPixelSize(R.styleable.YTopBottom_bottomTextSize, dip2px(context, 0));
        int topTextColor = array.getColor(R.styleable.YTopBottom_topTextColor, 0);
        int bottomTextColor = array.getColor(R.styleable.YTopBottom_bottomTextColor, 0);

        int topIconSize = array.getDimensionPixelSize(R.styleable.YTopBottom_topIconSize, dip2px(context, 30));
        int bottomIconSize = array.getDimensionPixelSize(R.styleable.YTopBottom_bottomIconSize, dip2px(context, 30));

        array.recycle();

        if (0 != topIcon){
            ivTop.setImageResource(topIcon);
            ivTop.setVisibility(VISIBLE);
        }else{
            ivTop.setVisibility(GONE);
        }
        if (YStringUtil.isNotEmpty(topText)){
            tvTop.setText(topText);
            tvTop.setVisibility(VISIBLE);
        }else{
            tvTop.setVisibility(GONE);
        }
        if (0 != bottomIcon){
            ivBottom.setImageResource(bottomIcon);
            ivBottom.setVisibility(VISIBLE);
        }else{
            ivBottom.setVisibility(GONE);
        }
        if (YStringUtil.isNotEmpty(bottomText)){
            tvBottom.setText(bottomText);
            tvBottom.setVisibility(VISIBLE);
        }else{
            tvBottom.setVisibility(GONE);
        }
        if (0 != topTextSize){
            tvTop.setTextSize(TypedValue.COMPLEX_UNIT_PX, topTextSize);
        }
        if (0 != topTextColor){
            tvTop.setTextColor(topTextColor);
        }
        if (0 != bottomTextSize){
            tvBottom.setTextSize(TypedValue.COMPLEX_UNIT_PX,bottomTextSize);
        }
        if (0 != bottomTextColor){
            tvBottom.setTextColor(bottomTextColor);
        }

        LinearLayout.LayoutParams topParams = new LinearLayout.LayoutParams(topIconSize, topIconSize);
        ivTop.setLayoutParams(topParams);
        LinearLayout.LayoutParams bottomParams = new LinearLayout.LayoutParams(bottomIconSize, bottomIconSize);
        ivBottom.setLayoutParams(bottomParams);
    }
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setTopText(String topText){
        tvTop.setText(topText);
        tvTop.setVisibility(VISIBLE);
    }






}
